package controllers

import dao._
import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}
import play.api.routing.JavaScriptReverseRouter
import shared.VarTool
import tool.FormTool
import utils.Utils
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 2019/4/28
 */
class AppController @Inject()(cc: ControllerComponents)(
  implicit val accountDao: AccountDao,
  implicit val userDao: UserDao
) extends AbstractController(cc) {

  def toIndex = Action { implicit request =>
    Ok(views.html.index())
  }

  def loginBefore = Action { implicit request =>
    Ok(views.html.login())
  }

  def login = Action.async { implicit request =>
    val data = FormTool.userForm.bindFromRequest().get
    accountDao.selectById1.zip(userDao.selectByUserData(data)).map { case (account, optionUser) =>
      if (data.name == account.account && data.password == account.password) {
        Redirect(routes.AdminController.userManageBefore()).addingToSession(VarTool.adminStr -> data.name)
      } else if (optionUser.isDefined) {
        val user = optionUser.get
        Redirect(routes.AppController.toIndex()).addingToSession(VarTool.userStr -> user.name, VarTool.idStr -> user.id.toString)
      } else {
        Redirect(routes.AppController.loginBefore()).flashing("info" -> "用户名或密码错误!", "class" -> Utils.errorClass)
      }
    }
  }

  def javascriptRoutes = Action { implicit request =>
    Ok(
      JavaScriptReverseRouter("jsRoutes")(
        controllers.routes.javascript.SickCaseController.get10Gene,
        controllers.routes.javascript.SickCaseController.getKnpb,
        controllers.routes.javascript.SickCaseController.getLymSnp,
        controllers.routes.javascript.SickCaseController.get68Snp,
        controllers.routes.javascript.SickCaseController.get68Cnv,
        controllers.routes.javascript.SickCaseController.getBasic,
        controllers.routes.javascript.SickCaseController.getBraf,

        controllers.routes.javascript.AdminController.getAllUser,
        controllers.routes.javascript.AdminController.userNameCheck,
        controllers.routes.javascript.AdminController.addUser,
        controllers.routes.javascript.AdminController.deleteUserById,
        controllers.routes.javascript.AdminController.getUserById,
        controllers.routes.javascript.AdminController.updateUser,

        controllers.routes.javascript.DataController.getAllData,

        controllers.adminC.routes.javascript.DataController.addByFile,
        controllers.adminC.routes.javascript.DataController.deletes,
        controllers.adminC.routes.javascript.DataController.deleteById,

        controllers.userC.routes.javascript.DataController.detailInfoBefore,
        controllers.userC.routes.javascript.DataController.getDataByBarcode,
        controllers.userC.routes.javascript.DataController.getAllDataByBarCode,
        controllers.userC.routes.javascript.DataController.getAllRefGeneByBarcode,
        controllers.userC.routes.javascript.DataController.getAllDataByBarcodeAndRefGene,
        controllers.userC.routes.javascript.DataController.getAllDataBySampleName,
        controllers.userC.routes.javascript.DataController.getAllDataByRefGene,
        controllers.userC.routes.javascript.DataController.getSingleColumnAll,


      )
    ).as("text/javascript")
  }


}
